package cn.jinzhu.netty.anli;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;

public class Server {
    public static void main(String[] args) {

        new ServerBootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {

                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                              //接送客户端数据 转成byte
                                ByteBuf m = (ByteBuf) msg;

                                ByteBuf buffer = ctx.alloc().buffer();
                                //将客户端数据处理后写入buffer
                                buffer.writeBytes(m);
                                //立马发送给客户端ctx.writeAndFlush(buffer);
                                ctx.writeAndFlush(buffer);
                            }
                        });
                    }
                }).bind(8080);
    }
}
